﻿Public Class FuntionandSub

  'Highlight Row
  Public Sub HighlightRow(ByVal dg As DataGridView, ByVal sCurRowID As String)
    Dim i As Integer = 0
    Dim dtTable As DataTable = dg.DataSource
    For Each dtRow As DataRow In dtTable.Rows
      If dtTable.Rows.Count > 0 Then
        If sCurRowID = dtRow("ID") Then
          dg.CurrentCell = dg.Item("Name", i)
        End If
      End If
      i += 1
    Next
  End Sub

  'Highlight Row
  Public Sub HighlightRow(ByVal dg As DataGridView, ByVal iCurRowID As Integer)
    Dim i As Integer = 0
    Dim dtTable As DataTable = dg.DataSource
    For Each dtRow As DataRow In dtTable.Rows
      If dtTable.Rows.Count > 0 Then
        If iCurRowID = dtRow("ID") Then
          dg.CurrentCell = dg.Item("Name", i)
        End If
      End If
      i += 1
    Next
  End Sub

  ' Get ID Max
  Public Function GetID_Max(ByVal tbTable As DataTable) As Integer
    Dim i As Integer = 0
    Dim IDMax As Integer = 0
    For Each dtRow As DataRow In tbTable.Rows
      If tbTable.Rows.Count > 0 Then
        If IDMax < tbTable.Rows(i)("ID") Then
          IDMax = tbTable.Rows(i)("ID")
        End If
      End If
      i += 1
    Next
    Return IDMax
  End Function

  'Search item on grid
  Public Shared Sub SearchItem(ByVal columnIndex As Integer, ByVal strKey As String, ByVal dg As StandardUI.ExDataGridView)
    If Not String.IsNullOrEmpty(strKey) Then
      Dim foundRow As Integer = SearchBinaryOnGrid(dg, columnIndex, strKey.ToUpper)
      If foundRow <> -1 Then
        dg.CurrentCell = dg.Rows(foundRow).Cells(1)
      End If
    End If
  End Sub

  'Search item on Grid by Search Binary method
  Public Shared Function SearchBinaryOnGrid(ByVal dg As StandardUI.ExDataGridView, ByVal columnIndex As Integer, ByVal sSearchString As String) As Integer
    Dim k, result As Integer
    Dim minIndex, maxIndex As Integer
    minIndex = 0
    maxIndex = dg.RowCount - 1
    k = -1
    result = -1
    Try
      Select Case maxIndex
        Case 0
          result = -1
        Case 1
          If String.Compare(sSearchString, dg.Rows(0).Cells(columnIndex).Value.ToString, True) = 0 Then
            result = 0
          Else
            result = -1
          End If
        Case Else
          While (minIndex <= maxIndex)
            k = (minIndex + maxIndex) / 2
            If String.Compare(sSearchString, dg.Rows(k).Cells(columnIndex).Value.ToString, True) = 0 Then
              result = k
              Exit While
            ElseIf String.Compare(sSearchString, dg.Rows(k).Cells(columnIndex).Value.ToString, True) < 0 Then
              maxIndex = k - 1
            Else
              minIndex = k + 1
            End If
          End While
          'Find exact Patient from neighbouring Patients
          Dim mink As Integer = k
          If result = -1 Then
            result = k
            For i As Integer = Math.Max(0, k - 1) To Math.Min(k + 1, dg.RowCount - 1)
              If String.Compare(sSearchString, dg.Rows(i).Cells(columnIndex).Value.ToString, True) > 0 Then
                result = i
                mink = i
              End If
            Next i
            Dim iMinLength As Integer
            Dim iCount As Integer = 0
            For i As Integer = mink To Math.Min(k + 1, dg.RowCount - 1)
              Dim sPatient As String = dg.Rows(i).Cells(columnIndex).Value.ToString
              iMinLength = Math.Min(sSearchString.Length, sPatient.Length)
              'Compare each character of string that need to find, and then save it.
              For j As Integer = iCount To iMinLength - 1
                Dim sCompareGrid As String = sPatient.Substring(0, iCount + 1)
                Dim sCompareFind As String = sSearchString.Substring(0, iCount + 1)
                If String.Compare(sCompareFind, sCompareGrid, True) = 0 Then
                  result = i
                  iCount += 1
                Else
                  Exit For
                End If
              Next
            Next
          End If
      End Select
    Catch ex As Exception
      result = -1
    End Try
    Return result
  End Function

End Class
